<script setup lang="ts">
import { RouterLink, RouterView } from 'vue-router'
import HelloWorld from './components/HelloWorld.vue'
import {ref} from 'vue'
import { computed } from 'vue'
import { onMounted } from 'vue'// 引入onMounted,用于localStorage

//添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务
const inputValue = ref('')//输入框绑定的变量
interface TodoItem {//声明一个TodoItem接口
  text: string
  iscompleted: boolean
}
const items = ref<TodoItem[]>([])//声明一个items数组,用于存储任务

//获取localStorage中的数据 
onMounted(() => {
    const savedItems = localStorage.getItem('todostorage');
    if (savedItems) {
        const parsedItems = JSON.parse(savedItems);
        // 确保每个项目都有正确的类型和属性
        items.value = parsedItems.map((item: any) => ({
            text: item.text || '',
            iscompleted: item.iscompleted || false
        }));
    }
});

//存储items数据到localStorage
function saveToLocalStorage() {
    const todoList=JSON.stringify(items.value);
    localStorage.setItem('todostorage',todoList);//保存数据
}

//示例

const a=['a','b','c'];
const b=JSON.stringify(a);//将a的数据转换为JISON格式的字符串
localStorage.setItem('test',b);//括号内，test为存储的名称，b为要存储在test里面的数据

//示例


const completed = computed(() => {
  // 计算属性，返回已完成的任务
  return items.value.filter(items => items.iscompleted)
})

const uncompleted = computed(() => {
  // 计算属性，返回未完成的任务
  return items.value.filter(items => !items.iscompleted)
})

function additems() {// 添加任务
  // 只有当输入不为空时才添加
  if (inputValue.value.trim()) {
      items.value.push({
      text: inputValue.value,
      iscompleted: false
    })
    // 添加后清空输入框
    inputValue.value = '';
    saveToLocalStorage();
  }
}

function del(index:number){
  items.value.splice(index,1)
  saveToLocalStorage();
}

//添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务添加任务//






//注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能//






//注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能注册功能//




//登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能//







//登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能登录功能//


</script>

<template>
	<div class="container">
    <div class="container">
      
    </div>
    <header>
      <h1>待办事项</h1>
    </header>

    <div class="input-area">
      <input type="text" class="task-input" v-model="inputValue" placeholder="添加新任务..." v-on:keyup.enter="additems()">
      <button class="add-btn" @click="additems()">添加</button>
    </div>

    <div class="task-list">

      <div v-if="uncompleted.length > 0">
      <div class="section-title">待完成 
          ({{ uncompleted.length }})
        </div>
        <ul class="todo" id="todo">
          <li v-for="(item,index) in uncompleted" :key="index">  
            <span class="text">{{ item.text }}</span>

            <div class="buttons">

              <button class="complete" @click="item.iscompleted = true" title="标记为完成">
                <svg viewBox="0 0 16 16" fill="currentColor">
                  <path d="M13.5 2L6 9.5 2.5 6 1 7.5l5 5 8-8L13.5 2z"/>
                </svg>
              </button>

              <button class="remove" @click="del(index)" title="删除任务">
                <svg viewBox="0 0 16 16" fill="currentColor">
                  <path d="M14 3h-4V2c0-.6-.4-1-1-1H7c-.6 0-1 .4-1 1v1H2c-.6 0-1 .4-1 1s.4 1 1 1h1v9c0 .6.4 1 1 1h8c.6 0 1-.4 1-1V5h1c.6 0 1-.4 1-1s-.4-1-1-1zM6 2h4v2H6V2zm6 12H4V5h8v9z"/>
                </svg>
              </button>
            </div><!-- buttons -->
          </li>
        </ul>
      </div>
      

      <div v-if="completed.length > 0">

      <div class="section-title">已完成 
        ({{ completed.length }})
      </div>

      <ul class="todo" id="completed">
        <li v-for="(item,index) in completed" :key="index">  
          <span class="text">{{ item.text }}</span>
            <div class="buttons">

              <button class="complete" @click="item.iscompleted = false" title="标记为未完成">
                <svg viewBox="0 0 16 16" fill="currentColor">
                  <path d="M13.5 2L6 9.5 2.5 6 1 7.5l5 5 8-8L13.5 2z"/>
                </svg>
              </button>

              <button class="remove" @click="del(index)" title="删除任务">
                <svg viewBox="0 0 16 16" fill="currentColor">
                  <path d="M14 3h-4V2c0-.6-.4-1-1-1H7c-.6 0-1 .4-1 1v1H2c-.6 0-1 .4-1 1s.4 1 1 1h1v9c0 .6.4 1 1 1h8c.6 0 1-.4 1-1V5h1c.6 0 1-.4 1-1s-.4-1-1-1zM6 2h4v2H6V2zm6 12H4V5h8v9z"/>
                </svg>
              </button>
              
            </div>
          </li>
        </ul>
      </div>
      
      <div class="empty-state" v-if="items.length === 0">
        <p>暂无任务，请添加新任务</p>
      </div>
    </div>
  </div>

</template>

<style>
@charset "UTF-8";
      * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
      font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    }
    
    body {
      background: linear-gradient(135deg, #ffffff 0%, #ffffff 100%);
      min-height: 100vh;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 40px 20px;
      color: #333;
    }

    #app {
      width: 100%;
      display: flex;
      justify-content: center;
    }
    
    .container {
      width: 100%;
      max-width: 600px;
      background: rgb(255, 255, 255);
      border-radius: 16px;
      box-shadow: 0 10px 30px rgba(82, 81, 81, 0.1);
      overflow: hidden;
    }

    header {
      background: #5f7a7c;
      color: white;
      padding: 20px;
      text-align: center;
    }

    h1 {
      font-weight: 600;
      font-size: 1.8rem;
      margin-bottom: 5px;
    }
    
    .input-area {
      padding: 20px;
      border-bottom: 1px solid #eee;
      display: flex;
      justify-content: space-between;
    }
    
    .task-input {
      flex: 1;
      padding: 14px;
      border: 2px solid #ddd;
      border-radius: 8px 0 0 8px;
      font-size: 1rem;
      outline: none;
      transition: border-color 0.3s;
    }
    
    .task-input:focus {
      border-color: #5f7a7c;
    }
    
    .add-btn {
      background: #5f7a7c;
      color: white;
      border: none;
      padding: 0 25px;
      border-radius: 0 8px 8px 0;
      cursor: pointer;
      font-weight: 600;
      transition: background 0.3s;
    }
    
    .add-btn:hover {
      background: #4a6062;
    }
    
    .task-list {
      padding: 10px 0;
      min-height: 200px;
      max-height: 400px;
      overflow-y: auto;
    }
    
    .section-title {
      padding: 10px 20px;
      color: #5f7a7c;
      font-weight: 600;
      border-bottom: 1px solid #eee;
      background-color: #f9f9f9;
    }
    
    ul.todo {
      width: 100%;
      padding: 0 20px;
    }

    ul.todo li {
      width: 100%;
      min-height: 50px;
      font-size: 14px;
      font-weight: 500;
      color: #444;
      line-height: 22px;
      background: #fff;
      border-radius: 8px;
      position: relative;
      box-shadow: 0px 1px 3px rgba(44, 62, 80, 0.10);
      margin: 0 0 10px 0;
      padding: 14px 100px 14px 14px;
      word-break: break-word;
      display: flex;
      align-items: center;
      transition: all 0.2s ease;
    }
    
    ul.todo li:hover {
      box-shadow: 0px 3px 6px rgba(44, 62, 80, 0.15);
      transform: translateY(-1px);
    }

    ul.todo li:last-of-type {
      margin: 0;
    }

    ul.todo li .buttons {
      width: 100px;
      height: 50px;
      position: absolute;
      top: 0;
      right: 0;
      display: flex;
    }

    ul.todo li .buttons button {
      width: 50px;
      height: 50px;
      background: none;
      border: 0px;
      box-shadow: none;
      outline: none;
      cursor: pointer;
      display: flex;
      align-items: center;
      justify-content: center;
      -webkit-appearance: none;
      -moz-appearance: none;
    }

    ul.todo li .buttons button:last-of-type:before {
      content: '';
      width: 1px;
      height: 30px;
      background: #edf0f1;
      position: absolute;
      top: 10px;
      left: 0;
    }

    ul.todo li .buttons button svg {
      width: 20px;
      height: 20px;
    }

    ul.todo li .buttons button.complete svg {
      color: #25b99a;
      transition: color 0.2s ease;
    }

    ul.todo li .buttons button.complete:hover svg {
      color: #1a8c72;
    }

    ul.todo li .buttons button.remove svg {
      color: #c0cecb;
      transition: color 0.2s ease;
    }

    ul.todo li .buttons button.remove:hover svg {
      color: #e85656;
    }

    ul.todo#completed li {
      opacity: 0.8;
      background-color: #f8f8f8;
    }
    
    ul.todo#completed li .text {
      text-decoration: line-through;
      color: #888;
    }

    .empty-state {
      text-align: center;
      padding: 30px 20px;
      color: #999;
    }
    
    .empty-state p {
      margin-top: 15px;
      font-size: 1.1rem;
    }
    
    @media (max-width: 600px) {
      .container {
        border-radius: 12px;
      }
      
      header {
        padding: 15px;
      }
      
      h1 {
        font-size: 1.5rem;
      }
      
      .input-area {
        padding: 15px;
      }
      
      ul.todo {
        padding: 0 15px;
      }
    }

</style>
